﻿using Prism.Commands;
using System.Windows.Input;
using System.ComponentModel;
using FrameWork.Data.Mapping;
using System.Collections.ObjectModel;
using MongoDB.Bson.Serialization.Attributes;
using SqlSugar;

namespace FrameWork.Data.ViewObject
{
    public class TestStepData : ModelBase
    {
        private long _TestRecordId;
        [Description("测试记录编号")]
        public long TestRecordId
        {
            get { return _TestRecordId; }
            set { _TestRecordId = value; OnPropertyChanged(nameof(TestRecordId)); }
        }
        private int _LoopIndex;
        /// <summary>
        /// 所属循环号
        /// </summary>
        [Description("所属循环号")]
        public int LoopIndex
        {
            get { return _LoopIndex; }
            set { _LoopIndex = value; OnPropertyChanged(nameof(LoopIndex)); }
        }
        /// <summary>
        /// 工步序号
        /// </summary>
        private int _StepIndex;
        /// <summary>
        /// 工步序号
        /// </summary>
        [Description("工步序号")]
        public int StepIndex
        {
            get { return _StepIndex; }
            set { _StepIndex = value; OnPropertyChanged(nameof(StepIndex)); }
        }
        /// <summary>
        /// 循环号
        /// </summary>
        private int _StepAccIndex;
        /// <summary>
        /// 累积工步序号
        /// </summary>
        [Description("累积工步序号")]
        public int StepAccIndex
        {
            get { return _StepAccIndex; }
            set { _StepAccIndex = value; OnPropertyChanged(nameof(StepAccIndex)); }
        }
        private int _StepRecordRangeBegin;
        /// <summary>
        /// 工步记录范围开始
        /// </summary>
        [Description("工步记录范围开始")]
        public int StepRecordRangeBegin
        {
            get { return _StepRecordRangeBegin; }
            set { _StepRecordRangeBegin = value; OnPropertyChanged(nameof(StepRecordRangeBegin)); }
        }
        private int _StepRecordRangeEnd;
        /// <summary>
        /// 工步记录范围截止
        /// </summary>
        [Description("工步记录范围截止")]
        public int StepRecordRangeEnd
        {
            get { return _StepRecordRangeEnd; }
            set { _StepRecordRangeEnd = value; OnPropertyChanged(nameof(StepRecordRangeEnd)); }
        }
        private string _StepName;
        /// <summary>
        /// 工步名称
        /// </summary>
        [Description("工步名称")]
        public string StepName
        {
            get { return _StepName; }
            set { _StepName = value; OnPropertyChanged(nameof(StepName)); }
        }
        /// <summary>
        /// 步骤时间
        /// </summary>
        private double? _StepTime;
        [Description("步骤时间")]
        public double? StepTime
        {
            get { return _StepTime; }
            set { _StepTime = value; OnPropertyChanged(nameof(StepTime)); }
        }
        private double? _Capacity;
        /// <summary>
        /// 容量
        /// </summary>
        [Description("容量")]
        public double? Capacity
        {
            get { return _Capacity; }
            set { _Capacity = value; OnPropertyChanged(nameof(Capacity)); }
        }
        private double? _ChargeCapacity;
        /// <summary>
        /// 充电容量
        /// </summary>
        [Description("充电容量")]
        public double? ChargeCapacity
        {
            get { return _ChargeCapacity; }
            set { _ChargeCapacity = value; OnPropertyChanged(nameof(ChargeCapacity)); }
        }
        /// <summary>
        /// 放电容量
        /// </summary>
        private double? _DischargeCapacity;
        [Description("放电容量")]
        public double? DischargeCapacity
        {
            get { return _DischargeCapacity; }
            set { _DischargeCapacity = value; OnPropertyChanged(nameof(DischargeCapacity)); }
        }
        /// <summary>
        /// 充电累计容量
        /// </summary>
        private double? _ChargeAccCapacity;
        [Description("充电累计容量")]
        public double? ChargeAccCapacity
        {
            get { return _ChargeAccCapacity; }
            set { _ChargeAccCapacity = value; OnPropertyChanged(nameof(ChargeAccCapacity)); }
        }
        /// <summary>
        /// 放电累积容量
        /// </summary>
        private double? _DischargeAccCapacity;
        [Description("放电累积容量")]
        public double? DischargeAccCapacity
        {
            get { return _DischargeAccCapacity; }
            set { _DischargeAccCapacity = value; OnPropertyChanged(nameof(DischargeAccCapacity)); }
        }
        private double? _Energy;
        /// <summary>
        /// 能量
        /// </summary>
        [Description("能量")]
        public double? Energy
        {
            get { return _Energy; }
            set { _Energy = value; OnPropertyChanged(nameof(Energy)); }
        }
        /// <summary>
        /// 充电能量
        /// </summary>
        private double? _ChargeEnergy;
        [Description("充电能量")]
        public double? ChargeEnergy
        {
            get { return _ChargeEnergy; }
            set { _ChargeEnergy = value; OnPropertyChanged(nameof(ChargeEnergy)); }
        }
        /// <summary>
        /// 放电能量
        /// </summary>
        private double? _DischargeEnergy;
        [Description("放电能量")]
        public double? DischargeEnergy
        {
            get { return _DischargeEnergy; }
            set { _DischargeEnergy = value; OnPropertyChanged(nameof(DischargeEnergy)); }
        }
        /// <summary>
        /// 充电累积能量
        /// </summary>
        private double? _ChargeAccEnergy;
        [Description("充电累积能量")]
        public double? ChargeAccEnergy
        {
            get { return _ChargeAccEnergy; }
            set { _ChargeAccEnergy = value; OnPropertyChanged(nameof(ChargeAccEnergy)); }
        }
        /// <summary>
        /// 放电累积能量
        /// </summary>
        private double? _DischargeAccEnergy;
        [Description("放电累积能量")]
        public double? DischargeAccEnergy
        {
            get { return _DischargeAccEnergy; }
            set { _DischargeAccEnergy = value; OnPropertyChanged(nameof(DischargeAccEnergy)); }
        }
        /// <summary>
        /// 开始时间
        /// </summary>
        private DateTime _StartTime;
        [Description("开始时间")]
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime StartTime
        {
            get { return _StartTime; }
            set { _StartTime = value; OnPropertyChanged(nameof(StartTime)); }
        }
        /// <summary>
        /// 结束时间
        /// </summary>
        private DateTime _EndTime;
        [Description("结束时间")]
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime EndTime
        {
            get { return _EndTime; }
            set { _EndTime = value; OnPropertyChanged(nameof(EndTime)); }
        }

        /// <summary>
        /// 充电中值电压
        /// </summary>
        [SugarColumn(IsNullable = true, DecimalDigits = 6, ColumnDescription = "充电中值电压")]
        public double? ChargeMidVolt { get; set; }
        /// <summary>
        /// 放电中值电压
        /// </summary>
        [SugarColumn(IsNullable = true, DecimalDigits = 6, ColumnDescription = "放电中值电压")]
        public double? DischargeMidVolt { get; set; }
        /// <summary>
        /// 平台电压
        /// </summary>
        [SugarColumn(IsNullable = true, DecimalDigits = 6, ColumnDescription = "平台电压")]
        public double? PlatformVoltage { get; set; }
        /// <summary>
        /// 平台容量
        /// </summary>
        [SugarColumn(IsNullable = true, DecimalDigits = 6, ColumnDescription = "平台容量")]
        public double? PlatformCapacity { get; set; }
        /// <summary>
        /// 平台时间
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "平台时间")]
        public double? PlatformTime { get; set; }
        /// <summary>
        /// 容量保持率
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "容量保持率")]
        public double? CapacityKeepRate { get; set; }
        /// <summary>
        /// 克容量
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "克容量")]
        public double? GramCapacity { get; set; }
        /// <summary>
        /// 中止条件
        /// </summary>
        [SugarColumn(IsNullable = true, Length = 200, ColumnDescription = "终止条件")]
        public string EndCondition { get; set; }
        /// <summary>
        /// DCIR1
        /// </summary>
        [SugarColumn(IsNullable = true, ColumnDescription = "DCIR1")]
        public double? DCIR1 { get; set; }

        #region ___协助属性___
        private bool _IsExpanded;
        [BsonIgnore]
        public bool IsExpanded
        {
            get { return _IsExpanded; }
            set { _IsExpanded = value; OnPropertyChanged(nameof(IsExpanded)); }
        }
        private ObservableCollection<TestDetailData> _ListTestDetailData;
        [BsonIgnore]
        public ObservableCollection<TestDetailData> ListTestDetailData
        {
            get { return _ListTestDetailData; }
            set { _ListTestDetailData = value; OnPropertyChanged(nameof(ListTestDetailData)); }
        }
        private TestDetailData _SelectDetailItem;
        [BsonIgnore]
        public TestDetailData SelectDetailItem
        {
            get { return _SelectDetailItem; }
            set { _SelectDetailItem = value; OnPropertyChanged(nameof(SelectDetailItem)); }
        }
        /// <summary>
        /// 所有测试数据
        /// </summary>
        /// 
        [BsonIgnore]
        public List<TestDetailData> TestDetailDatas { get; set; }
        /// <summary>
        /// 是否接续循环
        /// </summary>
        [BsonIgnore]
        [SugarColumn(IsIgnore = true, ColumnDescription = "是否接续循环")]
        public bool IsSplicing { get; set; }
        #endregion

        #region ___翻页属性___
        protected int _pageIndex = 1;
        [BsonIgnore]
        public virtual int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; OnPropertyChanged(nameof(PageIndex)); }
        }

        protected int _pageSize = 500;
        [BsonIgnore]
        public virtual int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; OnPropertyChanged(nameof(PageSize)); }
        }

        protected int _totalPages = 0;
        [BsonIgnore]
        public virtual int TotalPages
        {
            get { return _totalPages; }
            set { _totalPages = value; OnPropertyChanged(nameof(TotalPages)); }
        }

        protected long _totalItems = 0;
        [BsonIgnore]
        public virtual long TotalItems
        {
            get { return _totalItems; }
            set { _totalItems = value; OnPropertyChanged(nameof(TotalItems)); }
        }
        public virtual void LoadItems()
        {
            var totalNumber = TestDetailDatas.Count;
            var listData = TestDetailDatas.OrderBy(p => p.Id).Skip(PageSize * (PageIndex - 1)).Take(PageSize);
            TotalPages = (int)Math.Ceiling(totalNumber / (double)PageSize);
            TotalItems = totalNumber;
            ListTestDetailData = new ObservableCollection<TestDetailData>(listData);
        }
        // <summary>
        // 翻页
        // </summary>
        public virtual ICommand GoPage
        {
            get
            {
                return new DelegateCommand<object>(obj =>
                {
                    LoadItems();
                });
            }
        }
        #endregion
    }
}